<?php
class TestCategories extends CActiveRecord {
	
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public function tableName() {
		return strtolower('PsyTestCategories');
	}
	
	public function rules() {
		return array(
            array('categoryName, categoryOrder, ', 'required'),
            array('categoryOrder', 'numerical')
		);
	}

    public function defaultScope() {
        return array(
            'order' => 'categoryOrder'
        );
    }
    public function attributeLabels() {
        return array(
            'categoryName' => '分类名称',
            'categoryOrder' => '排序',
        );
    }

    public function afterDelete() {
        TestTopics::model()->updateAll(array('psyTestCategoryId' => 0), 'psyTestCategoryId = '.$this->psyTestCategoryId);
    }

    public function relations() {
        return array(
            'topics' => array(self::HAS_MANY, 'TestTopics', 'psyTestCategoryId',
                'joinType'=> 'LEFT JOIN'
            )
        );
    }

    public function updatePsytestCount() {
        $this->psyTestCount = TestTopics::model()->count('psyTestCategoryId = '.$this->psyTestCategoryId);
        $this->save();
    }
}
